package kj.androidsync;

import roboguice.activity.RoboActivity;
import roboguice.event.EventManager;
import roboguice.event.Observes;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.inject.Inject;

public class AndroidSyncActivity extends RoboActivity {

	@InjectView(R.id.counter)
	TextView counter;

	@InjectView(R.id.start)
	Button start;

	@Inject
	EventManager eventManager; /* RoboGuice class for managing events */

	@Inject
	Adder adder; /* Class that internally increments a counter */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	protected void handleCounterUpdate(@Observes final UpdateCounterEvent updateCounterEvent) {
		/* UI thread cannot be modified from an outer thread */
		
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				counter.setText(Integer.toString(updateCounterEvent.getCounter()));
			}
		});
	}
	
	protected void handleTestAlreadyStarted(@Observes TestAlreadyStartedEvent testAlreadyStartedEvent) {
		Toast.makeText(this, testAlreadyStartedEvent.getMessage(), Toast.LENGTH_LONG).show();
	}

	public void start(View v) {
		this.adder.startAddition();
	}

}